package com.renfei.filtercamera;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.cgfay.uitls.utils.PermissionUtils;
import com.renfei.cameralibrary.PreviewEngine;
import com.renfei.cameralibrary.activity.CameraActivity;
import com.renfei.cameralibrary.listener.OnPreviewCaptureListener;
import com.renfei.cameralibrary.model.AspectRatio;
import com.renfei.filterlibrary.glfilter.resource.FilterHelper;
import com.renfei.filterlibrary.glfilter.resource.MakeupHelper;
import com.renfei.filterlibrary.glfilter.resource.ResourceHelper;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    private String[] storagePermission = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (PermissionUtils.permissionChecking(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            initResources();
        } else {
            checkPermissions(storagePermission, REQUEST_CODE_STORAGE);
        }

        // Example of a call to a native method
        TextView tv = findViewById(R.id.sample_text);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                previewCamera();
            }
        });
        tv.setText(stringFromJNI());
    }

    private void initResources() {
        new Thread(() -> {
            ResourceHelper.initAssetsResource(MainActivity.this);
            FilterHelper.initAssetsFilter(MainActivity.this);
//            MakeupHelper.initAssetsMakeup(MainActivity.this);
        }).start();
    }

    /**
     * 打开预览界面
     */
    private void previewCamera() {
        if (PermissionUtils.permissionsChecking(this, new String[]{
                Manifest.permission.CAMERA,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.RECORD_AUDIO
        })) {
            jumpCameraActivity();
        } else {
            checkPermissions(new String[]{Manifest.permission.CAMERA,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.RECORD_AUDIO}, REQUEST_CODE);
        }
    }


    private static final int REQUEST_CODE = 0;
    private static final int REQUEST_CODE_STORAGE = 1;

    private void checkPermissions(String[] Permission, int requestCode) {
        PermissionUtils.requestPermissions(this, Permission, requestCode);
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_CODE) {
            jumpCameraActivity();
        } else if (requestCode == REQUEST_CODE_STORAGE) {
            initResources();
        }
    }

    private void jumpCameraActivity() {
        PreviewEngine.from(this)
                .setCameraRatio(AspectRatio.Ratio_16_9)
                .showFacePoints(false)
                .showFps(true)
                .backCamera(true)
                .setPreviewCaptureListener(new OnPreviewCaptureListener() {
                    @Override
                    public void onMediaSelectedListener(String path, int type) {
                        Log.e(TAG, "onMediaSelectedListener : " + path);
                        if (type == OnPreviewCaptureListener.MediaTypeVideo) {
                            Intent intent = new Intent(MainActivity.this, VideoEditActivity.class);
                            intent.putExtra(Constant.VIDEO_PATH, path);
                            startActivity(intent);
                        }
                    }
                })
                .startPreview();
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
}
